package com.yl.message.service.impl

import com.alibaba.fastjson.JSONObject
import com.yl.common.ErrorCode
import com.yl.common.UserType
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.message.common.MessageType
import com.yl.message.service.ISendMessageService
import com.yl.message.service.getMessageService
import com.yl.message.vo.ToUserMessageVo
import com.yl.users.services.ICompanyUserService
import com.yl.users.services.ISysCompanyService
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service


@Service("sendMessageService")
open class SendMessageServiceImpl(
	private val sysCompanyService: ISysCompanyService,
	private val companyUserService: ICompanyUserService,
) : ISendMessageService {

	companion object {
		private val log = LoggerFactory.getLogger(SendMessageServiceImpl::class.java)!!
	}


	override fun sendMessageToCompanyUser(
		vo: ToUserMessageVo,
		method: (companyId: Long, userId: Long, name: String) -> JSONObject?
	): ResponseBody {
		//成功计数
		var successNumber = 0

		val company = companyUserService.getCacheCompany(vo.companyId)
		if (company == null) {
			log.error("指定的公司ID是一个无效的ID.值：{}", vo.companyId)
			return returnError(ErrorCode.PARAM_EMPTY, "未拿到公司配置")
		}

		//循环企业可以使用的通知方式
		for (name in sysCompanyService.getSendMessageNames(UserType.Company, vo.companyId)) {
			//如果此通知方式无法找到实现类.跳过
			val msgObj = getMessageService(name) ?: continue

			for (userId in vo.userIds) {
				//过滤当前用户可以发送的消息渠道
				val config = method(vo.companyId, userId, name) ?: continue

				if (!msgObj.send(config, userId, vo.body).bodyIsError()) {
					successNumber += 1
					if (vo.successOnce) break
				}
			}
		}

		return returnSuccess(successNumber)
	}

	override fun sendMessageToCompanyUserHaveType(
		config: JSONObject,
		msgBody: JSONObject,
		messageType: MessageType,
		vararg userIds: Long
	): ResponseBody {
		val messageService = getMessageService(messageType.value) ?: return returnError(
			ErrorCode.DATA_IS_NOT_FIND,
			"未找到发送信息发送方法"
		)
		for (userId in userIds) {
			messageService.send(config, userId, msgBody)
		}
		return returnSuccess()

	}

	override fun sendMessageToCustom(
		vo: ToUserMessageVo,
		method: (companyId: Long, userId: Long, name: String) -> JSONObject?
	): ResponseBody {
		//成功计数
		var successNumber = 0

		val company = companyUserService.getCacheCompany(vo.companyId)
		if (company == null) {
			log.error("指定的公司ID是一个无效的ID.值：{}", vo.companyId)
			return returnError(ErrorCode.PARAM_EMPTY, "未拿到公司配置")
		}

		//循环企业可以使用的通知方式
		for (name in sysCompanyService.getSendMessageNames(UserType.Custom, vo.companyId)) {
			//如果此通知方式无法找到实现类.跳过
			val msgObj = getMessageService(name) ?: continue

			for (userId in vo.userIds) {
				//过滤当前用户可以发送的消息渠道
				val config = method(vo.companyId, userId, name) ?: continue

				if (!msgObj.send(config, userId, vo.body).bodyIsError()) {
					successNumber += 1
					if (vo.successOnce) break
				}
			}
		}

		return returnSuccess(successNumber)
	}


	override fun sendMessageToAdmin(vo: ToUserMessageVo): ResponseBody {
		TODO("Not yet implemented")
	}
}
